package com.guangaiboen.attendance.core.dispatcher.executor;

import com.guangaiboen.attendance.core.dispatcher.constants.RequestTypes;
import com.guangaiboen.attendance.core.dispatcher.context.DispatcherContext;
import com.guangaiboen.attendance.core.dispatcher.exception.DispatcherException;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;

import java.net.URISyntaxException;
import java.util.List;

/**
 * Get请求
 * @param <T> 返回类型
 */
public abstract class GetHttpDispatcherExecutor<T> extends AbstractGenericHttpDispatcherExecutor<T> {

	@Override
	protected HttpRequestBase createHttpRequest(String url, DispatcherContext dispatcherContext) {
		List<NameValuePair> nvps = getDispatcherParameters(url,dispatcherContext);
		try {
			URIBuilder uriBuilder = new URIBuilder(url);
			uriBuilder.setParameters(nvps);
			return new HttpGet(uriBuilder.build());
		} catch (URISyntaxException e) {
			throw new DispatcherException(e);
		}
	}

	/**
	 * 拼参数  必须实现
	 * @param url
	 * @param dispatcherContext
	 * @return
	 */
	protected abstract List<NameValuePair> getDispatcherParameters(String url, DispatcherContext dispatcherContext);

	@Override
	protected String getRequestTypes() {
		return RequestTypes.GET.getRequestCode();
	}
}
